package database

import (
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	"gintest/core/config"
	"github.com/garyburd/redigo/redis"
)

var SqlDB *gorm.DB
var RedisDB redis.Conn
func Init() {
	//mysql
	var err error
	if config.Sconfig.Read("db","open")=="true"{
		SqlDB, err = gorm.Open("mysql", config.Sconfig.Read("db","user") + ":"+config.Sconfig.Read("db","pass")+"@tcp("+config.Sconfig.Read("db","host")+":"+config.Sconfig.Read("db","port")+")/"+config.Sconfig.Read("db","base")+"?charset=utf8&parseTime=True&loc=Local")
		//SqlDB, err = gorm.Open("mysql", "gintest:gintest@/gintest?charset=utf8&parseTime=True&loc=Local")
		if err != nil {
			panic("连接数据库失败")
		}
	}
	//redis
	if config.Sconfig.Read("redis","open")=="true"{
		RedisDB, err = redis.Dial("tcp", config.Sconfig.Read("redis","host")+":"+config.Sconfig.Read("redis","port"))
		if err != nil {
			panic("连接Redis失败")
		}

		if len(config.Sconfig.Read("redis","pass"))!=0{
			err=RedisDB.Send("auth",config.Sconfig.Read("redis","pass"))
			if err !=nil {
				panic("Redis认证失败")
			}
		}
	}
}